/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.model.core;

import com.blandware.atleap.common.util.CompositeID;
import com.blandware.atleap.common.util.ConvertUtil;

import java.io.Serializable;

/**
 * <p>Class that holds application resource in form <code>localized_key=value</code>
 * where <code>localized_key</code> is unique identifier, which consists of <code>key<code> itself and
 * locale identifier
 * </p>
 * <p><a href="ApplicationResource.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.15 $ $Date: 2006/08/06 07:03:35 $
 * @hibernate.class table="`al_core_app_resource`" lazy="false"
 */
public class ApplicationResource extends BaseObject {

	//~ Instance fields ========================================================

	/**
	 * Composite ID of this resource
	 */
	protected ApplicationResourceID id;

	/**
	 * Value of this resource (associated with key and locale)
	 */
	protected byte[] value;

	/**
	 * Version of object
	 */
	protected Long version;

	//~ Constructors ===========================================================

	/**
	 * Creates new instance of ApplicationResource
	 */
	public ApplicationResource() {
	}

	/**
	 * Creates new instance of ApplicationResource
	 *
	 * @param locale Locale of message
	 * @param key    Key to associate value with
	 * @param value  Value to associate with specified ID
	 */
	public ApplicationResource(ContentLocale locale, String key, String value) {
		this.id = new ApplicationResourceID(locale, key);
		this.value = ConvertUtil.convertToByteArray(value);
	}

	//~ Methods ================================================================

	/**
	 * Returns ID of this resource
	 *
	 * @return ID of resource
	 * @hibernate.id generator-class="assigned" class="com.blandware.atleap.model.core.ApplicationResource$ApplicationResourceID"
	 */
	public ApplicationResourceID getId() {
		return id;
	}

	/**
	 * Sets ID of this resource
	 *
	 * @param id resource ID
	 */
	public void setId(ApplicationResourceID id) {
		this.id = id;
	}

	/**
	 * Returns version of this resource
	 *
	 * @return version
	 * @hibernate.version column="`version`" type="long" unsaved-value="null"
	 */
	public Long getVersion() {
		return version;
	}

	/**
	 * Sets version of this resource
	 *
	 * @param version new version
	 */
	public void setVersion(Long version) {
		this.version = version;
	}

    /**
     * Returns value corresponding to this resource
     *
     * @return value
     * @hibernate.property type="com.blandware.atleap.persistence.hibernate.util.BinaryBlobType"
     * column="`resource_value`" not-null="false" unique="false"
     * length="${database.blob.size.max}"
     */
    public byte[] getValue() {
        return value;
    }

    /**
     * Sets value of this resource
     *
     * @param value new value
     */
    public void setValue(byte[] value) {
        this.value = value;
    }

	public boolean equals(Object o) {
		if ( this == o ) {
			return true;
		}
		if ( !(o instanceof ApplicationResource) ) {
			return false;
		}

		final ApplicationResource applicationResource = (ApplicationResource) o;

		if ( !id.equals(applicationResource.id) ) {
			return false;
		}

		return true;
	}

	public int hashCode() {
		return id.hashCode();
	}

	/**
	 * <p>Class represents composite ID of ApplicationResource.</p>
	 * Composite ID consists of two fields: <ul>
	 * <li>key - key in bundle</li>
	 * <li>language - bundle language</li>
	 * </ul>
	 *
	 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
	 * @version $Revision: 1.15 $ $Date: 2006/08/06 07:03:35 $
	 */
	public static class ApplicationResourceID implements CompositeID, Serializable, Comparable {

		/**
		 * Key of this resource in bundle
		 */
		protected String key;

		/**
		 * Locale associated with resource
		 */
		protected ContentLocale locale;

		/**
		 * Creates new instance of ApplicationResourceID
		 */
		public ApplicationResourceID() {
		}

		/**
		 * Creates new instance of ApplicationResourceID
		 *
		 * @param locale Locale of resourcef x
		 * @param key    Key of resource
		 */
		public ApplicationResourceID(ContentLocale locale, String key) {
			this.key = key;
			this.locale = locale;
		}

		/**
		 * Returns key
		 *
		 * @return the key
		 * @hibernate.id
		 * @hibernate.property column="`resource_key`" not-null="true" unique="false" length="188"
		 */
		public String getKey() {
			return key;
		}

		/**
		 * Sets key of resource
		 *
		 * @param key the key
		 */
		public void setKey(String key) {
			this.key = key;
		}

		/**
		 * Returns locale
		 *
		 * @return the locale
		 * @hibernate.many-to-one class="com.blandware.atleap.model.core.ContentLocale" column="`locale_identifier`" outer-join="false"
		 */
		public ContentLocale getLocale() {
			return locale;
		}

		/**
		 * Sets locale of resource
		 *
		 * @param locale the locale
		 */
		public void setLocale(ContentLocale locale) {
			this.locale = locale;
		}

		/**
		 * Compares this application resource to another one. The comparison
		 * is made in two steps: first on <code>key</code>, than on <code>locale</code>,
		 * if keys are equal.
		 *
		 * @param o the object to compare to
		 * @return a negative integer, zero, or a positive integer as this object
		 *         is less than, equal to, or greater than the specified object.
		 */
		public int compareTo(Object o) {
			if ( !(o instanceof ApplicationResourceID) ) {
				throw new ClassCastException("Cannot compare instance of '" + getClass().getName() + "' to the instance of '" + o.getClass().getName() + "'");
			}

			ApplicationResourceID id = (ApplicationResourceID) o;
			if ( key.equalsIgnoreCase(id.key) ) {
				return locale.compareTo(id.locale);
			} else {
				return key.compareTo(id.key);
			}
		}

		public boolean equals(Object o) {
			if ( this == o ) {
				return true;
			}
			if ( !(o instanceof ApplicationResourceID) ) {
				return false;
			}

			final ApplicationResourceID applicationResourceID = (ApplicationResourceID) o;

			if ( !key.equals(applicationResourceID.key) ) {
				return false;
			}
			if ( !locale.equals(applicationResourceID.locale) ) {
				return false;
			}

			return true;
		}

		public int hashCode() {
			int result;
			result = key.hashCode();
			result = 29 * result + locale.hashCode();
			return result;
		}

	}


}
